/*
 * #%L
 * Client
 * %%
 * Copyright (C) 2011 Ractoc.com
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.ractoc.opengamefinder.client.plugins.nifty;

import org.apache.log4j.Logger;

import com.jme3.niftygui.NiftyJmeDisplay;
import com.ractoc.opengamefinder.client.GuiContainer;
import com.ractoc.opengamefinder.client.plugins.messages.GuiMessage;
import com.ractoc.pffj.api.BasePlugin;
import com.ractoc.pffj.api.BasePluginMessageResult;
import com.ractoc.pffj.api.PluginLoaderException;
import com.ractoc.pffj.api.PluginMessage;
import com.ractoc.pffj.api.PluginMessageResultListener;

import de.lessvoid.nifty.Nifty;
import org.apache.commons.lang.StringUtils;

/**
 * GUI plugin which uses Nifty to create the GUI and interact with the user.
 * @author ractoc
 * @version 0.1
 */
public class NiftyPlugin extends BasePlugin {

    private static Nifty nifty;
    private static Logger logger = Logger.getLogger(NiftyPlugin.class.getName());
    private static NiftyJmeDisplay niftyDisplay;
    private static String avatarFolder;

    /**
     * Default constructor.
     */
    public NiftyPlugin() {
    }

    /**
     * Get the instance of the current Nifty, used by various screen related classes.
     * @return Instance of the current Nifty, used by various screen related classes.
     */
    public static Nifty getNifty() {
        return nifty;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final void load() throws PluginLoaderException {

        if (StringUtils.isEmpty(avatarFolder)) {
            throw new PluginLoaderException("Mandatory parameter avatarFolder missing.");
        } else {
            logger.info("avatarFolder=" + avatarFolder);
        }

        niftyDisplay = new NiftyJmeDisplay(GuiContainer.getInstance().getAssetManager(), GuiContainer.getInstance().getInputManager(), GuiContainer.getInstance().getAudioRenderer(), GuiContainer.getInstance().getGuiViewPort());
        nifty = niftyDisplay.getNifty();
        GuiContainer.getInstance().getGuiViewPort().addProcessor(niftyDisplay);

    }

    /**
     * {@inheritDoc}
     * Currently only supports one Message, CuiMessage.
     */
    @Override
    public final void processMessage(final PluginMessage msg, final PluginMessageResultListener listener) {
        BasePluginMessageResult result = null;
        logger.debug("received message: " + msg.getClass().getName());
        if (msg instanceof GuiMessage) {
            final GuiMessage guiMsg = (GuiMessage) msg;
            logger.debug("actual GUI message: " + guiMsg.getPayload().getClass().getName());
            result = guiMsg.getType().getScreen().processMessage(guiMsg.getPayload());
        }
        listener.receivedResult(result);
    }

    /**
     * Sets the avatar folder. This is the folder where OGF looks to see if there 
     * are possible avatars to choose from during registration. This is set from the PFfJ config file.
     * @param avatarFolderParam The avatar folder.
     */
    public final void setAvatarFolder(String avatarFolderParam) {
        avatarFolder = avatarFolderParam;
    }

    /**
     * Sets the avatar folder. This is the folder where OGF looks to see if there 
     * are possible avatars to choose from during registration. This is set from the PFfJ config file.
     * @return The avatar folder.
     */
    public static String getAvatarFolder() {
        return avatarFolder;
    }
}
